package com.wb3.organizer.service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.wb3.organizer.dao.TaskDAO;
import com.wb3.organizer.model.Account;
import com.wb3.organizer.model.Task;

public class TaskService {
	
	private TaskDAO dao;

	public TaskDAO getDao() {
		return dao;
	}

	public void setDao(TaskDAO dao) {
		this.dao = dao;
	}
	
	public void saveTask(Task task){
		dao.save(task);
	}
	
	public void updateTask(Task task){
		dao.update(task);
	}
	
	public void delete(Task task){
		dao.delete(task);
	}
	
	public List<Task> getTaskByTime(Account account, boolean justToday){
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String currentDate = format.format(new Date());
		List<Task> tasks = dao.getAllByAccount(account);
		List<Task> tasksOut = new ArrayList<Task>();
		for(int i=0; i<tasks.size(); i++){
			if((justToday && currentDate.equalsIgnoreCase(format.format(tasks.get(i).getDue()))) || !justToday){
				tasksOut.add(tasks.get(i));
			}
		}
		return tasksOut;
	}

	public Task getNote(int task_id) {
		return dao.getTaskById(task_id);
	}
	
}
